import os

from django.shortcuts import render, HttpResponse, redirect

from kuacha.settings import BASE_DIR
from .models import *
from django.core.paginator import Paginator


# Create your views here.
# 添加作者
def addHoot(request):
    if request.method == "GET":
        return render(request, "addHoot.html")
    if request.method == "POST":
        name = request.POST.get("name")
        age = request.POST.get("age")
        sex = request.POST.get("sex")
        hobby = request.POST.get("hobby")
        content = request.POST.get("content")
        try:
            icon = request.FILES.get("icon")
            with open(os.path.join(BASE_DIR, "static", str(icon.name)), "wb") as f:
                for chunk in icon.chunks():
                    f.write(chunk)
        except Exception as a:
            return HttpResponse("未上传图片")
        else:
            a = Author(name=name, age=age, sex=sex, hobby=hobby, content=content, icon=str(icon))
            a.save()
            return redirect("Shower")


# 添加图书
def addAuter(request):
    if request.method == "GET":
        author1 = Author.objects.all()
        return render(request, "addAuter.html", context={"author1": author1})
    if request.method == "POST":
        btype = request.POST.get("btype")
        book = request.POST.get("book")
        author = request.POST.get("author")
        content = request.POST.get("content")
        time = request.POST.get("time")
        a = Book(btype=btype, book=book, author_id=author, content=content, time=time)
        a.save()
        return redirect("Shower")


# 展示
# def show(request):
#     pag = Book.objects.all()
#     page = request.GET.get("page")
#     paginator = Paginator(pag, 3)
#     now = paginator.get_page(page)
#     next = now.number + 1
#     last = now.number - 1
#     max = paginator.num_pages
#     small = 1
#     if now.number == max:
#         next = max
#
#     if now.number == small:
#         last = small
#
#     return render(request, "show.html", context={"now": now, "next": next, "last": last, "max": max})


def Shower(request):
    pag = Book.objects.all()
    page = request.GET.get("page")
    paginator = Paginator(pag, 3)
    now = paginator.get_page(page)
    next = now.number + 1
    last = now.number - 1
    max = paginator.num_pages
    small = 1
    if now.number == max:
        next = max
    if now.number == small:
        last = small
    return render(request, "Shower.html", context={"now": now, "next": next, "last": last, "max": max})

# 作者
def Rooter(request, id):
    a = Author.objects.get(id=id)
    return render(request, "Rooter.html", {"a": a})


# 删除作者

def deleA(request, id):
    a = Author.objects.get(id=id)
    a.delete()
    return redirect("Shower")


# 删除图书

def deleB(request, id):
    ad = Book.objects.get(id=id)
    ad.delete()
    return redirect("Shower")


# 修改作者
def Regter(request, id):
    if request.method == "GET":
        return render(request, "Regter.html")
    if request.method == "POST":
        name = request.POST.get("name")
        age = request.POST.get("age")
        sex = request.POST.get("sex")
        icon = request.FILES.get("icon")
        try:
            with open(os.path.join(BASE_DIR, "static", str(icon)), "wb") as f:
                for chunk in icon.chunks():
                    f.write(chunk)
        except Exception as a:
            return redirect("Shower")
        else:
            hobby = request.POST.get("hobby")
            content = request.POST.get("content")
            a = Author.objects.get(id=id)
            a.name = name
            a.age = age
            a.sex = sex
            a.icon = str(icon)
            a.hobby = hobby
            a.content = content
            a.save()
            return redirect("Shower")


# 修改图书
def Regttare(request, id):
    if request.method == "GET":
        a = Author.objects.all()
        return render(request, "Regttare.html", {"a": a})

    if request.method == "POST":
        btype = request.POST.get("btype")
        book = request.POST.get("book")
        author = request.POST.get("author")
        content = request.POST.get("content")
        time = request.POST.get("time")
        az = Book.objects.get(id=id)
        az.btype = btype
        az.book = book
        az.author_id = author
        az.content = content
        az.time = time
        az.save()
        return redirect("Shower")
